// new-impress.js
var app = getApp();
var constract = require('../../../../utils/constract.js');
var proof = require('../../../../utils/proof.js');
Page({

  data: {
    newImp: '',
  },

  onLoad: function (options) {
    var id = options.id;
    this.setData({
      id:id
    })
  },

  sliceVal: function(val){
    let zhVal = val.match(/[\u4e00-\u9fa5]/g) || [];
    let enVal = val.match(/[a-z0-9]/ig) || [];
    // let emjVal = val.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/ig) || [];
    
    let total = zhVal.length*2 + enVal.length;
    let diff = total - 20;

    if(diff > 0) {
      val = val.substr(0,val.length-1);
      console.log(val);
      this.sliceVal(val);
    } else {
      this.setData({
        newImp: val
      })
    }
  },

  getLength: function(e) {
    let val = e.detail.value;
    if(/[^\u4e00-\u9fa5|a-z|0-9]/ig.test(val)){
      val = val.replace(/[^\u4e00-\u9fa5|a-z|0-9]/ig,'');
      this.setData({
        newImp: val
      })
    }
    
    let zhVal = val.match(/[\u4e00-\u9fa5]/g) || [];
    let enVal = val.match(/[a-z0-9\s]/ig) || [];
    // let emjVal = val.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/ig) || [];
    
    let total = zhVal.length*2 + enVal.length;

    if(total>20){
      this.sliceVal(val);
    }

  },

  submit: function(e){
    var name = e.detail.value.impression_name;
    if(!proof.trim(name)){
      wx.showModal({
        title: '提示',
        content: '印象不能为空',
        showCancel: false
      });
      return
    }
    if (wx.showLoading) {
      wx.showLoading({
        title: '保存中',
        mask: true,
      });
    };
    var id=this.data.id;
    app.Verification(function (token) {
      wx.request({
        url: constract.impressionAddOrEdit,
        header:{token:token},
        data: { impression_uid: id, impression_name:name},
        success:function(res){
          console.log(res);
          if (res.data.code==14000){
            if (wx.hideLoading) {
              wx.hideLoading();
            };
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            });
            return;
          }
          if (wx.hideLoading) {
            wx.hideLoading();
          };
          wx.navigateBack();
        }
      })
    })
  }
})